#!/usr/bin/python3
# ******************************************************************************
# Copyright (c) Huawei Technologies Co., Ltd. 2021-2021. All rights reserved.
# licensed under the Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
#     http://license.coscl.org.cn/MulanPSL2
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
# PURPOSE.
# See the Mulan PSL v2 for more details.
# ******************************************************************************/
# !/usr/bin/python_learn
# -*- coding: UTF-8 -*-

import smtplib
import ssl
from email.mime.text import MIMEText
from email.utils import formataddr

my_sender = 'liheavy6@gmail.com'  # 发件人邮箱账号
my_pass = 'lhw@2hyz'  # 发件人邮箱密码
my_user = 'lihaiwei8@huawei.com'  # 收件人邮箱账号，我这边发送给自己


def mail():
    ret = True

    msg = MIMEText('Test', 'plain')
    msg['From'] = formataddr(("lhw-gmail", my_sender))  # 括号里的对应发件人邮箱昵称、发件人邮箱账号
    msg['To'] = formataddr(("lhw-hw", my_user))  # 括号里的对应收件人邮箱昵称、收件人邮箱账号
    msg['Subject'] = "发送邮件测试"  # 邮件的主题，也可以说是标题

    ssl_context = ssl.create_default_context()
    server = smtplib.SMTP_SSL("smtp.gmail.com", 465, context=ssl_context)
    # server.ehlo()
    # server.starttls()
    server.login(my_sender, my_pass)  # 括号中对应的是发件人邮箱账号、邮箱密码
    server.sendmail(my_sender, [my_user, ], msg.as_string())  # 括号中对应的是发件人邮箱账号、收件人邮箱账号、发送邮件
    server.quit()  # 关闭连接

    return ret


if __name__ == '__main__':
    ret = mail()
    if ret:
        print("邮件发送成功")
    else:
        print("邮件发送失败")
